package com.hp.btc.util.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

public class SendFileBySFTP {
	
	private static final Logger logger = LoggerFactory.getLogger(SendFileBySFTP.class);
	
	private Session session;
	private ChannelSftp channelSftp;
	
	public SendFileBySFTP() {
	}
	
	public SendFileBySFTP(String user, String password, String host, String port){
		try{
			createConnectionSFTP(user, password, host, Integer.parseInt(port));
		}catch(Exception exception){
			logger.error(" constructor SendFileBySFTP ", exception);
		}
	}
	
	public void sendSftpFile(File fileInput, String destination) throws IOException {
		logger.info("fileInput="+fileInput.toString()+",  destination="+destination);
		try {
			InputStream inputStream = new FileInputStream(fileInput);
			this.channelSftp.cd(destination);
			this.channelSftp.put(inputStream, fileInput.getName(), ChannelSftp.OVERWRITE);
			inputStream.close();
			logger.info("Send file close");
		}catch (SftpException e) {
			logger.error("Error-", e);
			throw new IOException("failed to write file", e);
		}
	}

	public void close() {
		logger.info("Cerrando..");
		if (this.session.isConnected()) {
			this.session.disconnect();
		}
		if (this.channelSftp != null) {
			this.channelSftp.exit();
        	this.channelSftp.disconnect();
        }
		logger.info("Close Final");
	}
	
	public void createConnectionSFTP(String user, String password, String host) throws Exception{
		createConnectionSFTP(user, password, host, 22);
	}
	
	public void createConnectionSFTP(String user, String password, String host, int port) throws Exception{
		logger.info("user="+user+", password="+password+", host="+host);
		JSch jsch=new JSch();
		
		if (session == null || !this.session.isConnected()) {
			session=jsch.getSession(user, host, port);
			session.setUserInfo(new OptimisticUserInfoImpl(password));
			session.setPassword(password);
			setConfig("no");
			this.session.connect();
			this.channelSftp = (ChannelSftp) this.session.openChannel("sftp");
		}
		if (this.channelSftp != null && !this.channelSftp.isConnected()) {
			this.channelSftp.connect();
		}
		
		logger.info("connected="+session.isConnected());
		
	}
	
	public void setConfig(String string){
		this.session.setConfig("StrictHostKeyChecking", string);
	}
	
	private static class OptimisticUserInfoImpl implements UserInfo, UIKeyboardInteractive {

		private final String password;

		public OptimisticUserInfoImpl(String password) {
			this.password = password;
		}

		public String getPassphrase() {
			return null; // pass
		}

		public String getPassword() {
			return this.password;
		}

		public boolean promptPassphrase(String string) {
			return true;
		}

		public boolean promptPassword(String string) {
			return true;
		}

		public boolean promptYesNo(String string) {
			return true;
		}

		public void showMessage(String string) {
		}

		public String[] promptKeyboardInteractive(String destination,
				String name, String instruction, String[] prompt, boolean[] echo) {
			return null;
		}
	}

	

}
